﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HtmlAgilityPack;
using System.Xml;
using System.Text;
using System.IO;

namespace APIIT_BPEL_Server.Utility
{
    public class Html_Parser
    {
        public string findNode(string htmlSource,string strTag,string endTag) 
        {
            string returnValue = null;
            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(htmlSource);
            var sb = new StringBuilder();
            var stringWriter = new StringWriter(sb);
            document.Save(stringWriter);
            string modifiedHtml = stringWriter.ToString();
            document.LoadHtml(modifiedHtml);
            var searchedValues = document.DocumentNode.SelectNodes(strTag);
            if (searchedValues != null)
            {
                foreach (var searchedvalue in searchedValues)
                {
                    if (endTag.Equals(""))
                    {
                        returnValue = searchedvalue.InnerText;
                    }
                    else
                    {
                        returnValue = searchedvalue.Attributes[endTag].Value;
                    }
                } 
            }
            return returnValue;
        }
    }
}